package org.sdsc.crms.service.security;

import java.util.List;

import org.sdsc.crms.dao.DeptDao;
import org.sdsc.crms.entity.security.Dept;
import org.sdsc.crms.service.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DeptManager extends EntityManager<Dept, Long> {

	@Autowired
	private DeptDao deptDao;

	@Override
	protected DeptDao getEntityDao() {
		return deptDao;
	}

	@Transactional(readOnly = true)
	public List<Dept> getDeptByParentId(Long parentId) {
		List<Dept> deptList = null;
		if (parentId == 0) {
			deptList = deptDao
					.find("select distinct d from Dept d left join fetch d.childDepts where d.parentDept is NULL order by d.indexs");
		} else {
			deptList = deptDao
					.find(
							"select distinct d from Dept d left join fetch d.childDepts where d.parentDept.id = ? order by d.indexs",
							parentId);
		}
		return deptList;
	}

	@Transactional(readOnly = true)
	public Dept get(Long id) {
		return deptDao.get(id);
	}

}
